<template>
<div class="flex-column">
  <sticky :className="'sub-navbar'">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" size="mini"  style="width: 200px;" class="filter-item" :placeholder="'名称'" v-model="listQuery.key">
      </el-input>
      
      <el-button class="filter-item" size="mini"  v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <permission-btn moduleName='currency' size="mini" v-on:btn-event="onBtnClicked"></permission-btn>
    </div>
     </sticky>
      <div class="app-container flex-item">
		
     <div class="bg-white" style="height: 100%;">
    <el-table  ref="mainTable" height="calc(100% - 52px)" :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
      style="width: 100%;" @row-click="rowClick"  @selection-change="handleSelectionChange">
          <el-table-column type="selection" align="center" width="55"></el-table-column>  
          <el-table-column min-width="80px" :label="'合同标题'">
            <template slot-scope="scope">
              <span>{{scope.row.contractTitle}}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="80px" :label="'对方单位'">
            <template slot-scope="scope">
              <span>{{scope.row.oppositeUnit}}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="80px" :label="'对方负责人'">
            <template slot-scope="scope">
              <span>{{scope.row.oppositeUnit}}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="80px" :label="'签订日期'">
            <template slot-scope="scope">
              <span>{{scope.row.signingDate}}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="80px" :label="'合同金额'">
            <template slot-scope="scope">
              <span>{{scope.row.contractAmount}}</span>
            </template>
          </el-table-column>

      <el-table-column align="center" :label="'操作'" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  type="primary" size="mini" @click="handleUpdate(scope.row)">标记已读</el-button>
        </template>
      </el-table-column>
    </el-table>
		<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="handleCurrentChange" />
	</div>
<el-dialog title="新增车辆" :visible.sync="dialogFormVisible">
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-col :span="11">
        <el-form-item label="厂牌型号" prop="brand">
          <el-input v-model="formData.contractNo" placeholder="请输入厂牌型号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="formData.contractTitle" placeholder="请输入司机姓名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
       <el-col :span="12">
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="formData.oppositeUnit" placeholder="请输入司机姓名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
       <el-col :span="12">
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="formData.oppositePerson" placeholder="请输入司机姓名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
       <el-col :span="12">
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="formData.contractCategory" placeholder="请输入司机姓名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
       <el-col :span="12">
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="formData.contractAmount" placeholder="请输入司机姓名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="保险到期时间" prop="insuranceEndTime">
          <el-date-picker v-model="formData.creationTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
            :style="{width: '100%'}" placeholder="请输入保险到期时间" clearable></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="所属公司" prop="company">
          <el-input v-model="formData.projectDescription" placeholder="请输入所属公司" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="车辆颜色" prop="carColor">
          <el-input v-model="formData.changeClause" placeholder="请输入车辆颜色" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="运营证号" prop="runNumber">
          <el-input v-model="formData.contractText" placeholder="请输入运营证号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="年检到期时间" prop="annualInspectionEndTime">
          <el-date-picker v-model="formData.signingDate" format="yyyy-MM-dd"
            value-format="yyyy-MM-dd" :style="{width: '100%'}" placeholder="请输入年检到期时间" clearable>
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="保养公里数" prop="maintainKilometre">
          <el-input v-model="formData.handler" placeholder="请输入保养公里数" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="车辆颜色" prop="carPhoto">
          <el-input v-model="formData.status" placeholder="请输入车辆颜色" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="11">
        <el-form-item label="保险卡照片" prop="insuranceCardPhoto">
          <el-input v-model="formData.approver" placeholder="请输入保险卡照片" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
  <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button size="mini" v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
              <el-button size="mini" v-else type="primary" @click="updateData">确认</el-button>
   </div>
</el-dialog>

  </div>
</div>
 
</template>

<script>
import * as currency from '@/api/currency'
import waves from '@/directive/waves' // 水波纹指令
import Sticky from '@/components/Sticky'
import permissionBtn from '@/components/PermissionBtn'
import Pagination from '@/components/Pagination'
import elDragDialog from '@/directive/el-dragDialog'
import extend from "@/extensions/delRows.js"
export default {
  name: 'currency',
  components: { Sticky, permissionBtn, Pagination },
  directives: {
    waves,
    elDragDialog
  },
  mixins: [extend],
  data() {
    return {
      multipleSelection: [], // 列表checkbox选中的值
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: { // 查询条件
        page: 1,
        limit: 20,
        key: undefined,
        appId: undefined
      },
      statusOptions: [
        { key: true, display_name: '停用' },
        { key: false, display_name: '正常' }
      ],
       formData : {
        id:'',
        contractNo: '',
        contractTitle: '',
        oppositeUnit: '',
        oppositePerson: '',
        contractCategory: '',
        signingDate: '',
        contractAmount: '',
        projectDescription: '',
        changeClause: '',
        contractText: '',
        handler: '',
        creationTime: '',
        status: '',
        approver: '',
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
       roleUsers: {
          dialogUserResource: false,
          Texts: '',
          rowIndex: -1,
          selectUsers: [],
          list: []
        },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        appId: [{ required: true, message: '必须选择一个应用', trigger: 'change' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  filters: {
    statusFilter(disable) {
      const statusMap = {
        false: 'color-success',
        true: 'color-danger'
      }
      return statusMap[disable]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    rowClick(row) {
      this.$refs.mainTable.clearSelection()
      this.$refs.mainTable.toggleRowSelection(row)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    onBtnClicked: function(domId) {
      console.log('you click:' + domId)
      switch (domId) {
        case 'btnAdd':
          this.handleCreate()
          break
        case 'btnEdit':
          if (this.multipleSelection.length !== 1) {
            this.$message({
              message: '只能选中一个进行编辑',
              type: 'error'
            })
            return
          }
          this.handleUpdate(this.multipleSelection[0])
          break
        case 'btnDel':
          if (this.multipleSelection.length < 1) {
            this.$message({
              message: '至少删除一个',
              type: 'error'
            })
            return
          }
          this.handleDelete(this.multipleSelection)
          break
        default:
          break
      }
    },
    getList() {
      this.listLoading = true
      currency.getList(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.count
        this.listLoading = false
        console.log(response.data)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val.page
      this.listQuery.limit = val.limit
      this.getList()
    },
    handleModifyStatus(row, disable) { // 模拟修改状态
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.disable = disable
    },
    resetTemp() {
      this.temp = {
        id:'',
        contractNo: '',
        contractTitle: '',
        oppositeUnit: '',
        oppositePerson: '',
        contractCategory: '',
        signingDate: '',
        contractAmount: '',
        projectDescription: '',
        changeClause: '',
        contractText: '',
        handler: '',
        creationTime: '',
        status: '',
        approver: '',
      }
    },
    handleCreate() { // 弹出添加框
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['elForm'].clearValidate()
      })
    },
    createData() { // 保存提交
    alert("createData")
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
           
          currency.add(this.formData).then(() => {
            this.list.unshift(this.formData)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) { // 弹出编辑框
      this.temp = Object.assign({}, row) // copy obj
      console.log(this.temp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['elForm'].clearValidate()
      })
    },
     updateData() { // 更新提交
      this.$refs['elForm'].validate((valid) => {
        if (valid) {
              this.formData.id=this.temp.id
          const tempData = Object.assign({}, this.formData)
          currency.update(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.formData.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.formData)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }, 
    handleDelete(rows) { // 多行删除
      currency.del(rows.map(u => u.id)).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          rows.forEach(row => {
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
            this.roleUsers.list.splice(index, 1)
          })
        })
    }
  }
}
</script>
<style>
	.dialog-mini .el-select{
		width:100%;
	}
</style>
